#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <QMessageBox>
#include <QFileDialog>
#include <QProcess>
#include <filesystem>
#include <QDebug>


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    loaded = false;

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::display(const QMap<QString, QString> &map)
{
    ui->le_lastname->setText(map["lastname"]);
    ui->le_firstname->setText(map["firstname"]);
    ui->le_hpmax->setText(map["hpMax"]);
    ui->le_mpmax->setText(map["mpMax"]);
    ui->le_spmax->setText(map["spMax"]);
    ui->le_luck->setText(map["luck"]);
    ui->le_talent->setText(map["talent"]);
    ui->le_reputation->setText(map["reputation"]);
    ui->le_life->setText(map["life"]);
    ui->le_moodmax->setText(map["moodMax"]);
    ui->le_healthmax->setText(map["healthMax"]);
    ui->le_energymax->setText(map["energyMax"]);
    ui->le_beauty->setText(map["beauty"]);
    ui->le_abilitypoint->setText(map["abilityPoint"]);
    ui->le_attack->setText(map["attack"]);
    ui->le_defense->setText(map["defense"]);
    ui->le_footspeed->setText(map["footSpeed"]);
    ui->le_crit->setText(map["crit"]);
    ui->le_critvalue->setText(map["critValue"]);
    ui->le_guard->setText(map["guard"]);
    ui->le_guardvalue->setText(map["guardValue"]);
    ui->le_movespeed->setText(map["moveSpeed"]);
    ui->le_physicfree->setText(map["physicalFree"]);
    ui->le_magicfree->setText(map["magicFree"]);
    ui->le_beauty->setText(map["beauty"]);
    ui->le_luck->setText(map["luck"]);
    ui->le_blade->setText(map["blade"]);
    ui->le_spear->setText(map["spear"]);
    ui->le_sword->setText(map["sword"]);
    ui->le_fist->setText(map["fist"]);
    ui->le_palm->setText(map["palm"]);
    ui->le_finger->setText(map["finger"]);
    ui->le_fire->setText(map["fire"]);
    ui->le_froze->setText(map["froze"]);
    ui->le_thunder->setText(map["thunder"]);
    ui->le_wind->setText(map["wind"]);
    ui->le_earth->setText(map["earth"]);
    ui->le_wood->setText(map["wood"]);
    ui->le_refineelixir->setText(map["refineElixir"]);
    ui->le_refineweapon->setText(map["refineWeapon"]);
    ui->le_geomancy->setText(map["geomancy"]);
    ui->le_symbol->setText(map["symbol"]);
    ui->le_herbal->setText(map["herbal"]);
    ui->le_mine->setText(map["mine"]);


}


void MainWindow::on_pushButton_3_clicked()
{
    QMessageBox::about(this, tr("关于"), tr("作者：Ritter\n警告：使用前请先备份存档，存档损失概不负责！"));
}

void MainWindow::on_pushButton_clicked()
{
    auto res = QFileDialog::getOpenFileName(this, tr("打开存档文件"));
    if(!res.isEmpty()){
        ui->centralwidget->setEnabled(false);
        filehandler = new FileHandler2(res, this);
        filehandler->loadFile();
        connect(filehandler, &FileHandler2::json_generated, this, [this](bool json_generated){
            if(json_generated){
                auto map = filehandler->loadJson();
                display(map);
            }else{
                QMessageBox::warning(this, tr("警告"), tr("请确认正确的存档文件！"));
            }
            ui->centralwidget->setEnabled(true);
            ui->pushButton_2->setEnabled(true);
        });
    }

}

void MainWindow::on_pushButton_2_clicked()
{
    QMap<QString, QString> map{
        {"lastname", ui->le_lastname->text()},
        {"firstname", ui->le_firstname->text()},
        {"hpMax", ui->le_hpmax->text()},
        {"mpMax", ui->le_mpmax->text()},
        {"spMax", ui->le_spmax->text()},
        {"luck", ui->le_luck->text()},
        {"talent", ui->le_talent->text()},
        {"reputation", ui->le_reputation->text()},
        {"life", ui->le_life->text()},
        {"moodMax", ui->le_moodmax->text()},
        {"healthMax", ui->le_healthmax->text()},
        {"energyMax", ui->le_energymax->text()},
        {"beauty", ui->le_beauty->text()},
        {"abilityPoint", ui->le_abilitypoint->text()},
        {"attack", ui->le_attack->text()},
        {"defense", ui->le_defense->text()},
        {"footSpeed", ui->le_footspeed->text()},
        {"crit", ui->le_crit->text()},
        {"critValue", ui->le_critvalue->text()},
        {"guard", ui->le_guard->text()},
        {"guardValue", ui->le_guardvalue->text()},
        {"physicalFree", ui->le_physicfree->text()},
        {"magicFree", ui->le_magicfree->text()},
        {"moveSpeed", ui->le_movespeed->text()},
        {"blade", ui->le_blade->text()},
        {"spear", ui->le_spear->text()},
        {"sword", ui->le_sword->text()},
        {"fist", ui->le_fist->text()},
        {"palm", ui->le_palm->text()},
        {"finger", ui->le_finger->text()},
        {"fire", ui->le_finger->text()},
        {"froze", ui->le_froze->text()},
        {"thunder", ui->le_thunder->text()},
        {"wind", ui->le_wind->text()},
        {"earth", ui->le_earth->text()},
        {"wood", ui->le_wood->text()},
        {"refineElixir", ui->le_refineelixir->text()},
        {"refineWeapon", ui->le_refineweapon->text()},
        {"geomancy", ui->le_geomancy->text()},
        {"symbol", ui->le_symbol->text()},
        {"herbal", ui->le_herbal->text()},
        {"mine", ui->le_mine->text()}
    };
    ui->centralwidget->setEnabled(false);
    filehandler->saveFile(map);
    connect(filehandler, &FileHandler2::save_done, this, [this](bool){
        QMessageBox::warning(this, tr("提醒"), tr("修改成功！"));
//        filehandler->disconnect(this);
        ui->centralwidget->setEnabled(true);
        disconnect(filehandler, &FileHandler2::save_done, this, nullptr);
    });

}
